﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileDB;
using System.Configuration;

namespace Rem.Objects.Forms.General
{
    public partial class UpdatePassword : Form
    {
        public UpdatePassword()
        {
            InitializeComponent();
            this.userNameText.Text = Customer.GetSelfCompany().Name;
            this.userNameText.Enabled = false;
            this.olderpasswordText.PasswordChar = '*';
            this.newPasswordText.PasswordChar = '*';
            this.newpasswordAgainText.PasswordChar = '*';
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.newpasswordAgainText.Text != this.newPasswordText.Text)
            {

                MessageBox.Show("新密码两次输入不相等！");
                return;
            }
            if (this.newpasswordAgainText.Text.Length <= 3)
            {
                MessageBox.Show("密码至少3位数！");
                return;
            }

            if (!Helper.MatchPassword(this.olderpasswordText.Text, Expectedpasswordstr))
            {
                MessageBox.Show("老密码输入错误！");
                return;
            }
            //save
            //ConfigurationManager.AppSettings.Set("password", Helper.GeneratePasswordStr(this.newPasswordText.Text));
            System.Configuration.Configuration config = 
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["password"].Value = Helper.GeneratePasswordStr(this.newPasswordText.Text);
            config.Save(ConfigurationSaveMode.Modified); 
            ConfigurationManager.RefreshSection("appSettings"); 
            ConfigurationManager.RefreshSection("appSettings");
            //密码下次启动时生效
            this.Close();
        }

        public string Expectedpasswordstr { get; set; }
    }
}
